package ar.edu.utn.frsf.cidisi.ginet.generation.pnml2wscdl.popup.actions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pnmlcoremodel.Page;
import org.w3._2005._10.cdl.CdlFactory;
import org.w3._2005._10.cdl.TChoreography;
import org.w3._2005._10.cdl.TExtensibleElements;

import ar.edu.utn.frsf.cidisi.ginet.formalization.FormalAssociation;
import ar.edu.utn.frsf.cidisi.ginet.formalization.FormalCBPLanguage;
import ar.edu.utn.frsf.cidisi.ginet.generation.pnml2wscdl.elementstemplate.WSCDLStructure;
import ar.edu.utn.frsf.cidisi.ginet.generation.pnml2wscdl.managers.EMFModelLoad;

public class WSCDLCreator {
	
	public static TExtensibleElements createPackage(Page page, TChoreography choreography){
		
		String pagename = getPageName(page);
		
		WSCDLStructure structure = getTemplateByElement(pagename, page,choreography, new EMFModelLoad().load());
		
		if(structure != null){
			return structure.createWSCDLElement();
		}
		
		return CdlFactory.eINSTANCE.createTExtensibleElements();
		

	}
	
	private static WSCDLStructure getTemplateByElement(String name, Page page, TChoreography choreography, FormalCBPLanguage language) {
		
		EList<FormalAssociation> formalAssociations = language.getFormalizedElements();
		
		for (FormalAssociation formalAssociation : formalAssociations) {
			if(formalAssociation.getMetamodelElement().getElement().get(0).getName().toUpperCase().equals(name.toUpperCase())){
				String className = formalAssociation.getFormalElement().getElement().getId();
				Class<?> classTemplate = null;
				try {
					classTemplate = Class.forName("ar.edu.utn.frsf.cidisi.ginet.generation.pnml2wscdl.elementstemplate." + className);
					return (WSCDLStructure)classTemplate.getConstructor(Page.class, TChoreography.class).newInstance(page, choreography);
				} catch (java.lang.Exception e) {
					return null;
				}
			}
		}
		return null;
	}
	
	private static String getPageName(Page page) {		
		String pageName = page.getName().getText();
		
		Pattern p = Pattern.compile("-?\\d+");
		Matcher m = p.matcher(pageName);
		String list = "";
		while (m.find()) {
		  list = list + m.group();
		}
		String result = pageName.replaceAll(list, "");
		if(result.toUpperCase().startsWith("INTERACTIONPATH")){
			return "InteractionProtocolWSCDL";
		}
		
		return getTemplateName(result);
	}

	private static String getTemplateName(String result) {
		if(result.toUpperCase().startsWith("AND")){
			return "AndWSCDL";
		}
		if(result.toUpperCase().startsWith("BUSINESSMESSAGE")){
			return "BusinessMessageWSCDL";
		}
		if(result.toUpperCase().startsWith("CANCEL")){
			return "CancelWSCDL";
		}
		if(result.toUpperCase().startsWith("IF")){
			return "IfWSCDL";
		}
		if(result.toUpperCase().startsWith("INTERACTIONPROTOCOL")){
			return "InteractionProtocolWSCDL";
		}
		if(result.toUpperCase().startsWith("LOOPUNTIL")){
			return "LoopUntilWSCDL";
		}
		if(result.toUpperCase().startsWith("LOOPWHILE")){
			return "LoopWhileWSCDL";
		}
		if(result.toUpperCase().startsWith("OR")){
			return "OrWSCDL";
		}
		if(result.toUpperCase().startsWith("XOR")){
			return "XorWSCDL";
		}
		return "";
	}

}
